package com.selfdriving.map;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baidu.mapapi.map.MapView;
import com.selfdriving.BaseApplication;
import com.selfdriving.R;

public class ScenicLocation extends Activity implements OnClickListener
{
	private ScenicLocitonMap map = null;
	public static ListView stepList = null;
	private List<String> step = new ArrayList<String>();
	public static BaseAdapter adapter = null;
	private Button requestLocButton;
	private MapView mapView;
	private ImageView imgBreak;
	private TextView textView;
	private ProgressDialog dialog;
	private Button topRight;
	BaseApplication mApplication;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		super.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.scenic_location_activity);
		mApplication = (BaseApplication) getApplication();
		dialog = new ProgressDialog(this);
		dialog.setMessage("正在定位，请稍等");
		dialog.show();
		initViews();
		initEvents();
		map = new ScenicLocitonMap(mApplication, this, requestLocButton,
				mapView, step, stepList, mApplication.geoPoint, textView,
				dialog);
	}

	protected void initViews()
	{
		// TODO Auto-generated method stub
		topRight = (Button) super.findViewById(R.id.top_right_but);
		imgBreak = (ImageView) super.findViewById(R.id.head_break);
		imgBreak.setVisibility(View.VISIBLE);
		requestLocButton = (Button) super.findViewById(R.id.scenic_but);
		mapView = (MapView) super.findViewById(R.id.scenic_bmapView);
		textView = (TextView) super.findViewById(R.id.top_text_middle);
		stepList = (ListView) super.findViewById(R.id.scenic_line_message);
		adapter = new MyStepAdapter(ScenicLocation.this);
		stepList.setAdapter(adapter);
		topRight.setVisibility(View.VISIBLE);
		topRight.setText("显示文字导航");
		textView.setText("前往目的地");
	}

	protected void initEvents()
	{
		// TODO Auto-generated method stub
		imgBreak.setOnClickListener(this);
		topRight.setOnClickListener(this);
	}

	@Override
	public void onClick(View v)
	{
		// TODO Auto-generated method stub
		switch (v.getId())
		{
		case R.id.head_break:
			finish();
			break;
		case R.id.top_right_but:
			Button but = (Button) v;
			if (but.getText().equals("收起文字导航"))
			{
				topRight.setText("显示文字导航");
				stepList.setVisibility(View.GONE);
			} else
			{
				topRight.setText("收起文字导航");
				stepList.setVisibility(View.VISIBLE);
			}
			break;
		}
	}

	private class MyStepAdapter extends BaseAdapter
	{
		LayoutInflater inflater;

		public MyStepAdapter(Context context)
		{
			inflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount()
		{
			// TODO Auto-generated method stub
			return step.size();
		}

		@Override
		public Object getItem(int position)
		{
			// TODO Auto-generated method stub
			return step.get(position);
		}

		@Override
		public long getItemId(int position)
		{
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			// TODO Auto-generated method stub
			RelativeLayout relativeLayout = (RelativeLayout) inflater.inflate(
					R.layout.scenic_location_item, null);
			TextView textView = (TextView) relativeLayout
					.findViewById(R.id.scenic_text);
			textView.setText(step.get(position));
			return relativeLayout;
		}

	}

	@Override
	protected void onPause()
	{
		map.mMapView.onPause();
		super.onPause();
	}

	@Override
	protected void onResume()
	{
		if (map != null)
		{
			map.mMapView.onResume();
		}
		super.onResume();
	}

	@Override
	protected void onDestroy()
	{
		// 退出时销毁定位
		if (mApplication.mLocClient != null)
			mApplication.mLocClient.stop();
		map.mMapView.destroy();
		super.onDestroy();
	}

	@Override
	protected void onSaveInstanceState(Bundle outState)
	{
		super.onSaveInstanceState(outState);
		map.mMapView.onSaveInstanceState(outState);

	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState)
	{
		super.onRestoreInstanceState(savedInstanceState);
		map.mMapView.onRestoreInstanceState(savedInstanceState);
	}

	@Override
	public void onBackPressed()
	{
		finish();
	}

}
